fix(parse): treat empty/typeless schema as "any" in transform_schema#1691
Open
WalkingDreams798 wants to merge 1 commit into
Open
fix(parse): treat empty/typeless schema as "any" in transform_schema#1691WalkingDreams798 wants to merge 1 commit into
WalkingDreams798 wants to merge 1 commit into
Conversation
`transform_schema` raised `ValueError("Schema must have a 'type', 'anyOf',
'oneOf', or 'allOf' field.")` for any sub-schema that pydantic emits as an empty
`{}` — i.e. an `Any`-typed field, or the items of an untyped `list` / `List[Any]`.
This made structured outputs unusable for very common models:
class Result(BaseModel):
summary: str
tags: list[Any] # or a bare `list`, or any `Any`-typed field
client.beta.messages.parse(..., output_format=Result)
# ValueError raised at request-build time, before the API is called
An empty `{}` is a valid, unconstrained ("any") JSON Schema. Instead of raising
when there is no `type`/combinator, leave the sub-schema untyped. Added tests for
the empty schema, untyped array items, an `Any` object property, and an
end-to-end pydantic model regression.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
transform_schema(the structured-output schema transformer used bymessages.parse/beta.messages.parse) raisedfor any sub-schema that pydantic emits as an empty
{}— namely anAny-typed field, or theitemsof an untypedlist/List[Any].Repro
The error is raised at request-build time, before any API call. Notably, an untyped
dict/Dict[str, Any]field does not trip it (it keepstype: object), so the failure is specific to untyped lists andAnyfields — surprising and easy to hit.Root cause: in the array branch,
items: {}recurses intotransform_schema({}), and a schema with notype/anyOf/oneOf/allOfhit theraise. The same happens for anAnyobject property.Change
An empty
{}is a valid, unconstrained ("any") JSON Schema. Instead of raising when there is notypeand no combinator, leave the sub-schema untyped (permissive). One-line behavior change insrc/anthropic/lib/_parse/_transform.py.Tests
Added to
tests/lib/_parse/test_transform.py:{}schema{"type": "array", "items": {}})Anyobject propertyList[Any]+Anyfield (regression for the original crash)Verification
uv run pytest tests/lib/_parse→ 26 passeduv run ruff check→ all checks passeduv run pyright(strict) on changed files → 0 errors, 0 warnings